package edu.cueb.wmis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.cueb.wmis.mapper.*;
import edu.cueb.wmis.pojo.*;
import edu.cueb.wmis.service.IOrderbillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzizhuo
 * @since 2021-04-19
 */
@Service
public class OrderbillServiceImpl extends ServiceImpl<OrderbillMapper, Orderbill> implements IOrderbillService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private OrderbillMapper orderbillMapper;
    @Autowired
    private OrderbillitemMapper orderbillitemMapper;
    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private ShelfMapper shelfMapper;
    @Autowired
    private HouseShelfMapper houseShelfMapper;
    @Autowired
    private HouseMapper houseMapper;
    @Override
    public boolean addOrderBill(Orderbill orderbill) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
        String prefixToday = date.format(dtf);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetails userDetails = (UserDetails)principal;
        String username = userDetails.getUsername();
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("username", username));
        orderbill.setOperatorId(admin.getId());
        orderbill.setInputDate(date);
        orderbill.setState(0);
        System.out.println(orderbill);
        //表是否为空
        //为空：添加当天日期+001
        //不为空：
            //判断最后一条订单的日期 是否 与 当天日期相等
                //若相等：最后一条订单号+1
                //若不想等：新建单号：当天日期+001
        if(orderbillMapper.selectCount(null)==0){
            orderbill.setBillNumber(prefixToday + "001");
        }else{
            List<Map<String, Object>> list = orderbillMapper.selectMaps(new QueryWrapper<Orderbill>().select("max(billNumber)"));
            String billNumber = list.get(0).get("max(billNumber)").toString();
            String prefix = billNumber.substring(0,8);
            System.out.println(prefix);
            if(prefix.equals(prefixToday)){
                orderbill.setBillNumber(String.valueOf(Long.valueOf(billNumber)+1));
            }else{
                System.out.println(2);
                orderbill.setBillNumber(prefixToday + "001");
            }
        }
        //System.out.println(orderbill);
        if(orderbillMapper.insert(orderbill) == 1 ){
            System.out.println("t");
            return true;
        }else{
            System.out.println("f");
            return false;
        }
    }

//    @Override
//    public RespBean getOrderItems(Integer obId) {
//        List<Orderbillitem> orderbillitems = orderbillitemMapper.selectList(new QueryWrapper<Orderbillitem>().eq("billId",obId));
//        if(orderbillitems!=null){
//            return RespBean.success("成功",orderbillitems);
//        }else{
//            return RespBean.success("失败");
//        }
//    }

    @Override
    public RespBean getOrderItemsByBillNumber(String billNumber) {
        Integer obId = orderbillMapper.getOrderItemsByBillNumber(billNumber);
        System.out.println(obId);
        List<Orderbillitem> orderbillitems = orderbillitemMapper.selectList(new QueryWrapper<Orderbillitem>().eq("billId",obId));
        System.out.println(orderbillitems);
        for(Orderbillitem item : orderbillitems){
            Good good = goodMapper.selectById(item.getGoodId());
            Integer shelfId = good.getShelfId();
            Shelf shelf = shelfMapper.selectById(shelfId);
            good.setSname(shelf.getName());
            HouseShelf hs = houseShelfMapper.selectOne(new QueryWrapper<HouseShelf>().eq("sid", shelfId));
            House house = houseMapper.selectById(hs.getHid());
            good.setHname(house.getName());
            item.setGood(good);
        }
        if(orderbillitems!=null){
            return RespBean.success("成功",orderbillitems);
        }else{
            return RespBean.success("失败");
        }
    }
}
